import * as hamcUtils from './gameUtils'

export function calculateKeno(serverSeed: string, clientSeed: string, nonce: number) {
	const num = 10
	const arrays = hamcUtils.getBytesGroups(serverSeed, clientSeed, nonce, num, 4)
	const remainingPositions = Array(40)
		.fill(0)
		.map((_, index) => index)
	const result = []
	for (let i = 0; i < num; i++) {
		const relativePosition = Math.floor(hamcUtils.calculateResult(arrays[i], 40 - i))
		const [absoluteKenoPosition] = remainingPositions.splice(relativePosition, 1)
		result.push(absoluteKenoPosition + 1)
	}
	return result
}

/**
 * Limbo-赌场种子转换为字节-前端展示公开计算过程使用
 * @param {string} serverSeed - 服务端种子
 * @param {string} clientSeed - 客户端种子
 * @param {number} nonce - 随机数
 * @returns {object} - 返回字节对象
 */
export function casinoSeedToBytes(serverSeed: string, clientSeed: string, nonce: number) {
	return hamcUtils.casinoSeedToBytes(serverSeed, clientSeed, nonce, 2)
}

/**
 * 字节到数字的计算过程用到的分组 Limbo 前端公开过程 1组，每组4个字节
 * @param {string} serverSeed - 服务端种子
 * @param {string} clientSeed - 客户端种子
 * @param {number} nonce - 随机数
 * @returns {array} - 返回字节数组
 */
export function bytesToNumberGroups(serverSeed: string, clientSeed: string, nonce: number) {
	return hamcUtils.getBytesGroups(serverSeed, clientSeed, nonce, 10, 4)
}
